package com.pan.listener;

import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SessionListener implements HttpSessionListener {
    private static Logger logger = LoggerFactory.getLogger(HttpSessionListener.class);
    //记录在线的用户数
    public Integer count = 0;

    @Override
    public synchronized void sessionCreated(HttpSessionEvent se) {
        logger.info("新用户上线了");
        count++;
        se.getSession().getServletContext().setAttribute("count",count);
    }

    @Override
    public synchronized void sessionDestroyed(HttpSessionEvent se) {
        logger.info("用户下线了");
        count--;
        se.getSession().getServletContext().setAttribute("count",count);
    }
}
